package com.controller;

import com.bean.dangBean.Employee;
import com.bean.dangBean.Role;
import com.service.EmployeeService;
import com.util.OrderAssistant;
import com.util.PageAssistant;
import com.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 界面业务控制器
 */
@Controller
@RequestMapping("emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @RequestMapping("login")
    public String check(Employee employee, HttpSession session){

        System.out.println(">>>   emp login");
        System.out.println(employee);


        List<Employee> employeeList = employeeService.find(employee);
        //判断, 是否登录成功
        if (employeeList.size() == 1){
            session.setAttribute("loginInfo", employeeList.get(0));
            session.setAttribute("userRole", employeeList.get(0).getRoleId());
            return "redirect:/main";
        }

        session.setAttribute("msg", "用户名或密码错误!");
        return "redirect:/login";
    }


    @RequestMapping("logoff")
    public String logoff(HttpSession session){
        System.out.println(">>> emp logoff");
        System.out.println("------------------------------------------------");

        //销毁会话
        session.invalidate();

        return "redirect:/login";
    }

    @RequestMapping("query")
    public String query(Model model, HttpSession session, EmployeeVo empQueryInfo,
                        PageAssistant<Employee> paEmployee, OrderAssistant order){
        System.out.println(">>> emp query");
        System.out.println("empQueryInfo为:"+empQueryInfo);
        System.out.println("------------------------------------------------");
        List<Role> roleList = employeeService.queryRole();
        model.addAttribute("roleList", roleList);
        System.out.println(roleList);
        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数

        //添加查询参数
        map.put("key", empQueryInfo.getRealName());

        //查询总记录数
        paEmployee.setCount(employeeService.count(map));

        //设置分页参数
        map.put("start", paEmployee.getStart());
        map.put("size", paEmployee.getSize());


        //查询当前页内容
        paEmployee.setItems(employeeService.query(map));

        //保存
        model.addAttribute("paEmployee", paEmployee);
        model.addAttribute("empQueryInfo", empQueryInfo.parse());


        System.out.println("empQueryInfo为:"+empQueryInfo);
        System.out.println("paEmployee为:"+paEmployee);
        return "page/emp/empList";
    }

    @RequestMapping("unique")
    public String unqiue(Model model,HttpSession session, EmployeeVo emp, String page){
        System.out.println(">>> emp unique");
        System.out.println(emp);
        System.out.println(page);
        System.out.println("------------------------------------------------");

        //查询
        List<Employee> empList = employeeService.find(emp.parse());
        List<Role> roleList = employeeService.queryRole();
        model.addAttribute("roleList", roleList);
        System.out.println(roleList);
        //保存
        session.setAttribute("empInfo", empList.get(0));
        model.addAttribute("empInfo", empList.get(0));
        System.out.println("empList为"+empList);
        //判断, page是否为null
//        page = page == null ? "detail.jsp" : page;
        if (page == null){
            page = "empDetail.html";
        }

        return "page/emp/" + page;
    }

    @RequestMapping("remove")
    public String remove(HttpSession session, Employee employee){
        System.out.println(">>> emp remove");
        System.out.println(employee);
        System.out.println("------------------------------------------------");

        //删除
        int count = employeeService.remove(employee);

        //保存
        String updateMsg = "删除成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "删除失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "redirect:/emp/query";

    }

    @RequestMapping("modify")
    public String modify(HttpSession session, EmployeeVo employeeVo,Model model){
        System.out.println(">>> emp modify");
        System.out.println(employeeVo);
        System.out.println("------------------------------------------------");

        //修改
        int count = employeeService.modify(employeeVo.parse());


        return "redirect:/emp/unique?id="+employeeVo.getId();
    }

    @RequestMapping("save")
    public String save(HttpSession session, EmployeeVo employeeVo){
        System.out.println(">>> emp add");
        System.out.println(employeeVo);
        System.out.println("------------------------------------------------");

        //添加
        int count = employeeService.save(employeeVo.parse());
        return "redirect:/emp/unique?userName="+employeeVo.getUserName();
        /*return "redirect:/emp/query?userName="+employeeVo.getUserName();*/
    }

    @RequestMapping("findSalary")

    public String findSalary(Model model, HttpSession session, EmployeeVo empQueryInfo,
                             PageAssistant<Employee> paSalary, OrderAssistant order) {
        System.out.println(">>> findSalary");

        System.out.println("------------------------------------------------");

        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数


        //查询总记录数
        paSalary.setCount(employeeService.count(map));

        //设置分页参数
        map.put("start", paSalary.getStart());
        map.put("size", paSalary.getSize());


        //查询当前页内容
        paSalary.setItems(employeeService.query(map));

        //保存
        model.addAttribute("paSalary", paSalary);
        model.addAttribute("empQueryInfo", empQueryInfo.parse());
        System.out.println(empQueryInfo);
        System.out.println(paSalary);
        return "page/emp/salary";
    }

}
